<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-list-add">
			<div class="form-group">	
				<label class="col-sm-3 control-label">显示顺序：</label>
				<div class="col-sm-8">
					<input id="dishSort" name="dishSort" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">菜品名称：</label>
				<div class="col-sm-8">
					<input id="dishName" name="dishName" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">菜品类别：</label>
				<div class="col-sm-8">
					<select id="typeId" name="typeId" class="form-control select2-hidden-accessible" >
						<option th:each="dishType:${dishTypes}" th:value="${dishType.typeId}" th:text="${dishType.typeName}" th:disabled="${dishType.status == '1'}"></option>
					</select>
					
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">菜品售价：</label>
				<div class="col-sm-8">
					<input id="dishPrice" name="dishPrice" class="form-control" type="number" placeholder="若启用用餐账户，并且扣费时使用累计订餐菜品单价，则请填写">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">报餐类型：</label>
				<div class="col-sm-8">
				    <div class="radio-box" th:each="dict : ${@dict.getType('daily_meals_type')}">
						<input type="radio" th:id="${dict.dictCode}" name="dinnerType" th:value="${dict.dictValue}" th:checked="${dict.isDefault == 'Y' ? true : false}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">状态：</label>
				<div class="col-sm-8">
				    <div class="radio-box" th:each="dict : ${@dict.getType('sys_normal_disable')}">
						<input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}" th:checked="${dict.isDefault == 'Y' ? true : false}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<input id="remark" name="remark" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">
				<div class="form-control-static col-sm-offset-9">
					<button type="submit" class="btn btn-primary">提交</button>
					<button onclick="$.modal.close()" class="btn btn-danger" type="button">关闭</button>
				</div>
			</div>
		</form>
	</div>
    <div th:include="include::footer"></div>
    <script th:src="@{/ajax/libs/select/select2.js}"></script>
    <script type="text/javascript">
		var prefix = ctx + "dailymenu/dishlist"

		$("#form-list-add").validate({
			rules:{
				dishName:{
        			required:true,
        		},
        		typeId:{
        			required:true,
        		},
        		dinnerType:{
        			required:true,
        		},
        		dishSort:{
        			required:true,
        		}
			},
			submitHandler: function(form) {
				add();
			}
		});
		
		function add() {
        	var dishId = $("input[name='dishId']").val();
        	var dishSort = $("input[name='dishSort']").val();
        	var dishPic = $("input[name='dishPic']").val();
        	var dishName = $("input[name='dishName']").val();
        	var dishPrice = $("input[name='dishPrice']").val();
        	var typeId = $.form.selectSelects("typeId");
        	var dinnerType = $("input[name='dinnerType']:checked").val();
        	var status = $("input[name='status']:checked").val();
        	var remark = $("input[name='remark']").val();
        	if([[${isPrice}]] && !dishPrice){
                layer.alert('您启用了用餐扣费的功能，必须输入菜品单价！',{icon: 0},function (index) {
                    $("input[name='dishPrice']").focus();
                    layer.close(index);
                });
                return
            }
        	$.ajax({
        		cache : true,
        		type : "POST",
        		url : ctx + "dailymenu/dishlist/add",
        		data : {
        			"dishId": dishId,
        			"dishSort": dishSort,
        			"dishPic": dishPic,
        			"dishName": dishName,
        			"dishPrice": dishPrice,
        			"typeId": typeId,
        			"dinnerType": dinnerType,
        			"status": status,
        			"remark": remark
        		},
        		async : false,
        		error : function(request) {
        			$.modal.alertError("系统错误");
        		},
        		success : function(data) {
        			$.operate.saveSuccess(data);
        		}
        	});
        }
		
		// h新建进入此页聚焦于第一输入框
		document.getElementsByName("dishSort")[0].focus()
		// h弹框自适应高度
		function isScrollY(addition) {
// 			如果有滚动条
		   	if(!(document.documentElement.scrollHeight > document.documentElement.clientHeight)){
		   		var alertHeight = document.querySelector("form").clientHeight+addition
				window.parent.$('.layui-layer-iframe').height(alertHeight)
				window.parent.$('iframe').height(alertHeight)
				$('html').height(alertHeight)
				$('body').height(alertHeight)
		   	}
		}
		isScrollY(70)
		
	</script>
</body>
</html>
